package com.remenote.remenoteback.context.interceptor;

import com.remenote.remenoteback.util.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * @BelongsProject: remenote-back
 * @BelongsPackage: com.remenote.remenoteback.context.interceptor
 * @Author: 23392
 * @CreateTime: 2024-03-18  14:57
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class HttpRequestInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String username = request.getHeader("username");
        ThreadLocalUtil.setUserName(username);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        ThreadLocalUtil.remove();
    }
}
